package com.taotao.item.service.impl;

import com.github.pagehelper.PageInfo;
import com.taotao.common.dto.TbItem;
import com.taotao.common.dto.TbItemDesc;
import com.taotao.common.dto.TbItemExample;
import com.taotao.common.jedis.JedisClient;
import com.taotao.common.mqclient.JmsClient;
import com.taotao.common.pojo.TaotaoResult;
import com.taotao.common.utils.IDUtils;
import com.taotao.common.utils.JsonUtils;
import com.taotao.common.utils.Pager;
import com.taotao.item.dao.TbItemDescMapper;
import com.taotao.item.dao.TbItemMapper;
import com.taotao.item.service.ItemService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;


/**
 * 商品管理Service
 * <p>Title: ItemServiceImpl</p>
 * <p>Description: </p>
 * <p>Company: www.itcast.cn</p> 
 * @version 1.0
 */
@Service
public class ItemServiceImpl implements ItemService {

	@Autowired
	private TbItemMapper itemMapper;
	@Autowired
	private TbItemDescMapper itemDescMapper;
//	@Autowired
//	private JmsTemplate jmsTemplate;
//	@Resource(name="itemAddtopic")
//	private Destination destination;
	@Autowired
	private JmsClient jmsClient;
	@Autowired
	private JedisClient jedisClient;
	
	@Value("${ITEM_INFO}")
	private String ITEM_INFO;
	@Value("${TIEM_EXPIRE}")
	private Integer TIEM_EXPIRE;
	
	@Override
	public TbItem getItemById(long itemId) {
		//查询数据库之前先查询缓存
		try {
			String json = jedisClient.get(ITEM_INFO + ":" + itemId  + ":BASE");
			if (StringUtils.isNotBlank(json)) {
				// 把json数据转换成pojo
				TbItem tbItem = JsonUtils.jsonToPojo(json, TbItem.class);
				return tbItem;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//缓存中没有查询数据库
		TbItem item = itemMapper.selectByPrimaryKey(itemId);
		try {
			//把查询结果添加到缓存
			jedisClient.set(ITEM_INFO + ":" + itemId  + ":BASE", JsonUtils.objectToJson(item));
			//设置过期时间，提高缓存的利用率
			jedisClient.expire(ITEM_INFO + ":" + itemId  + ":BASE", TIEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return item;
	}

	@Override
	public PageInfo<TbItem> getItemList(int page, int rows) {
		//设置分页信息
//		PageHelper.startPage(page, rows);
		//执行查询
		TbItemExample example = new TbItemExample();
//		List<TbItem> list = itemMapper.selectByExample(example);
		PageInfo<TbItem> pageInfo = Pager.runPage(page, rows, () -> itemMapper.selectByExample(example));
		//取查询结果
//		PageInfo<TbItem> pageInfo = new PageInfo<>(list);
//		EasyUIDataGridResult result = new EasyUIDataGridResult();
//		result.setRows(list);
//		result.setTotal(pageInfo.getTotal());
		//返回结果
		return pageInfo;
	}

	@Override
	public TaotaoResult addItem(TbItem item, String desc) throws Exception {
		//生成商品id
		final long itemId = IDUtils.genItemId();
		//补全item的属性
		item.setId(itemId);
		//商品状态，1-正常，2-下架，3-删除
		item.setStatus((byte) 1);
		item.setCreated(new Date());
		item.setUpdated(new Date());
		//向商品表插入数据
		itemMapper.insert(item);
		//创建一个商品描述表对应的pojo
		TbItemDesc itemDesc = new TbItemDesc();
		//补全pojo的属性
		itemDesc.setItemId(itemId);
		itemDesc.setItemDesc(desc);
		itemDesc.setUpdated(new Date());
		itemDesc.setCreated(new Date());
		//向商品描述表插入数据
        try {
            itemDescMapper.insert(itemDesc);
            jmsClient.sendTopic(itemId + "");
            //返回结果
            return TaotaoResult.ok();
        }catch(Exception e){
            e.printStackTrace();
        }finally {
            throw new Exception();
        }
        //向Activemq发送商品添加消息
//		jmsTemplate.send(destination, new MessageCreator() {
//
//			@Override
//			public Message createMessage(Session session) throws JMSException {
//				//发送商品id
//				TextMessage textMessage = session.createTextMessage(itemId + "");
//				return textMessage;
//			}
//		});
	}

	@Override
	public TbItemDesc getItemDescById(long itemId) {
		//查询数据库之前先查询缓存
		try {
			String json = jedisClient.get(ITEM_INFO + ":" + itemId  + ":DESC");
			if (StringUtils.isNotBlank(json)) {
				// 把json数据转换成pojo
				TbItemDesc tbItemDesc = JsonUtils.jsonToPojo(json, TbItemDesc.class);
				return tbItemDesc;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//缓存中没有查询数据库
		TbItemDesc itemDesc = itemDescMapper.selectByPrimaryKey(itemId);
		try {
			//把查询结果添加到缓存
			jedisClient.set(ITEM_INFO + ":" + itemId  + ":DESC", JsonUtils.objectToJson(itemDesc));
			//设置过期时间，提高缓存的利用率
			jedisClient.expire(ITEM_INFO + ":" + itemId  + ":DESC", TIEM_EXPIRE);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return itemDesc;
	}

}
